package com.yy.comdemo.page

import android.util.Log
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.itemsIndexed
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.yy.comdemo.R
import com.yy.comdemo.controller.NavRoute
import com.yy.comdemo.entity.MapCardItem

@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
@Composable
fun MockLocationPage (navController: NavHostController) {
    val locations = remember {
        mutableStateListOf(
            MapCardItem("001"),
            MapCardItem("002"),
            MapCardItem("003"),
            MapCardItem("004"),
            MapCardItem("005"),
        )
    }
    val selPosition = remember {
        mutableStateOf(-1)
    }
    val showDelDlg = remember {
        mutableStateOf(false)
    }

    fun changeEdit(iIndex: Int) {
        for ((index, location) in locations.withIndex()) {
            location.isEdit = iIndex == index
        }
    }

    @Composable
    fun showDelConfirmDlg () {
        var selIndex:Int = -1
        var selItem: MapCardItem? = null
        for ((index, location) in locations.withIndex()) {
            if (location.isEdit) {
                selItem = location
                selIndex = index
            }
        }
        AlertDialog(
            title = { Text(text = "提示") },
            text = { Text(text = "是否确认删除该模拟地点，操作不可逆！") },
            onDismissRequest = {
                showDelDlg.value = false
                selItem?.isEdit = false
//                    onDismissRequest()
            },
            confirmButton = {
                TextButton(
                    onClick = {
                        locations.removeAt(selIndex)
                        if (selPosition.value > selIndex) {
                            selPosition.value = selPosition.value - 1
                        }
                        showDelDlg.value = false
                    }
                ) {
                    Text("确认")
                }
            },
            dismissButton = {
                TextButton(
                    onClick = {
                        showDelDlg.value = false
                        selItem?.isEdit = false
                    }
                ) {
                    Text("取消")
                }
            })
    }

    if (showDelDlg.value) showDelConfirmDlg()

    Scaffold (
        bottomBar = {
            BottomAppBar(
                containerColor = MaterialTheme.colorScheme.primaryContainer,
                contentColor = MaterialTheme.colorScheme.primary,
            ) {
                TextButton(onClick = { /*TODO*/ }) {
                    Column (
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        Icon(
                            painter = painterResource(id = R.drawable.menu_location),
                            contentDescription = "模拟",
                            Modifier.width(32.dp)
                        )
                        Text(
                            modifier = Modifier
                                .fillMaxWidth(),
                            textAlign = TextAlign.Center,
                            text = "模拟",
                        )
                    }
                }
            }
        },
//            floatingActionButton = {
//                FloatingActionButton(onClick = {
//
//                }) {
//                    Icon(Icons.Default.Add, contentDescription = "Add")
//                }
//            }
    ) {innerPadding ->
        Column(
            modifier = Modifier
                .padding(innerPadding),
            verticalArrangement = Arrangement.spacedBy(16.dp),
        ) {
            Text(
                modifier = Modifier.padding(8.dp),
                text = "位置模拟",
                fontSize = 32.sp,
                color = MaterialTheme.colorScheme.primary
            )
            LazyVerticalGrid(
                columns = GridCells.Fixed(2),
                contentPadding = PaddingValues(horizontal = 8.dp, vertical = 4.dp),
                verticalArrangement = Arrangement.spacedBy(8.dp),
                horizontalArrangement = Arrangement.spacedBy(8.dp),
            ) {
                item {
                    Card (
                        elevation = CardDefaults.cardElevation(
                            defaultElevation = 6.dp,
                            pressedElevation = 2.dp
                        ),
                        colors = CardDefaults.cardColors(
                            containerColor = MaterialTheme.colorScheme.primary,
                        ),
                    ) {
                        Row (
                            Modifier
                                .fillMaxWidth()
                                .height(120.dp),
                            verticalAlignment = Alignment.CenterVertically,
                            horizontalArrangement = Arrangement.Center
                        ) {
                            Icon(
                                Icons.Default.Add,
                                contentDescription = "Add",
                                Modifier
                                    .width(48.dp)
                                    .height(48.dp)
                                    .clickable {
                                        navController.navigate(NavRoute.BMAP.path)
                                    }
                                ,
                                tint = Color.White,
                            )
                        }
                        Text(
                            text = "新增",
                            Modifier.padding(horizontal = 2.dp, vertical = 10.dp),
                            color = Color.White
                        )
                    }
                }
                itemsIndexed(locations.toList()) {index, lt ->
                    Card (
                        elevation = CardDefaults.cardElevation(
                            defaultElevation = 6.dp
                        ),
                        colors = CardDefaults.cardColors(
                            containerColor = if (lt.isEdit) Color.Black.copy(alpha = 0.4f) else if (index == selPosition.value) MaterialTheme.colorScheme.onPrimaryContainer else MaterialTheme.colorScheme.primaryContainer,
                        ),
                    ) {
                        Box {
                            Column {
                                Image(
                                    modifier = Modifier
                                        .fillMaxWidth()
                                        .height(120.dp)
                                        .combinedClickable(
                                            onClick = {
                                                selPosition.value = index
                                                Log.v("TAG", "我点击了")
                                            },
                                            onLongClick = {
                                                Log.v("TAG", "我在长按哦")
                                                if (selPosition.value != index) {
                                                    changeEdit(index)
                                                }
                                            }
                                        )
                                    ,
                                    contentScale = ContentScale.Crop,
                                    painter = painterResource(R.drawable.map),
                                    contentDescription = lt.posName,
                                )
                                Text(
                                    text = lt.posName,
                                    Modifier.padding(horizontal = 2.dp, vertical = 10.dp),
                                    color = if (index == selPosition.value) Color.White else Color.Black,
                                )
                            }
                            if (lt.isEdit)
                                Card (
                                    colors = CardDefaults.cardColors(
                                        containerColor = Color.Black.copy(alpha = 0.4f)
                                    )
                                ) {
                                    Column (
                                        Modifier
                                            .fillMaxWidth()
                                            .fillMaxHeight(),
                                        horizontalAlignment = Alignment.CenterHorizontally,
                                        verticalArrangement = Arrangement.SpaceBetween,
                                    ) {
                                        Button(
                                            modifier = Modifier.fillMaxWidth(0.75f),
                                            onClick = {  }
                                        ) {
                                            Text("修改", fontSize = 12.sp)
                                        }
                                        Button(
                                            modifier = Modifier.fillMaxWidth(0.75f),
                                            colors = ButtonDefaults.buttonColors(
                                                containerColor = Color.Red
                                            ),
                                            onClick = {
                                                showDelDlg.value = true
                                            }
                                        ) {
                                            Text("删除", fontSize = 12.sp)
                                        }
                                        OutlinedButton(
                                            modifier = Modifier.fillMaxWidth(0.75f),
                                            onClick = {
                                                lt.isEdit = false
                                            }
                                        ) {
                                            Text("取消", fontSize = 12.sp, color = Color.White)
                                        }
                                    }
                                }
                        }
                    }
                }
            }
        }
    }
}